package com.yanqu.road.server.cmd.monopolymarket.office;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.monopolymarket.office.log.MonopolyMarketOfficeUserSeatLogData;
import com.yanqu.road.entity.monopolymarket.office.log.MonopolyMarketUserPhysicalChangeLogData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.monopolymarket.MonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/20 10:25
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_OFFICE_ACTION_FROM_CROSS, desc = "大宋榷署撤销/抢夺 位置 跨服返回区服 协议")
public class MonopolyMarketOfficeActionFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketOfficeActionToGameServerMsg resp = MonopolyMarketProto.MonopolyMarketOfficeActionToGameServerMsg.parseFrom(packet.getBytes());
        Property property = null;
        if(resp.getType() == 0){
            // 解除预扣除
            int needConsumeGold = MonopolyMarketMgr.calSitDownConsumeGold(player);
            if(needConsumeGold > 0){
                property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(needConsumeGold));
                player.getModule(PreReduceModule.class).restorePreReduce(property);
            }
        }
        if(resp.getStcMsg().getRet() == 0){
            int now = DateHelper.getCurrentSecond();
            MonopolyMarketModule marketModule = player.getModule(MonopolyMarketModule.class);
            if(resp.getType() == 0){
                // 入座
                if(property != null){
                    player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.MonopolyMarket, eLogMoneyType.MonopolyMarketOfficeConsume);
                }
                marketModule.baseData.setOfficeRobTimes(marketModule.baseData.getOfficeRobTimes() + 1);
                if(resp.getStcMsg().getIsSuccess()){
                    // 成功抢夺
                    MonopolyMarketMgr.refreshUser(player,true,false);

                    LogMgr.addMonopolyMarketOfficeUserSeatLogData(new MonopolyMarketOfficeUserSeatLogData(player.getUserId(),marketModule.baseData.getOfficeCurPos(),marketModule.baseData.getOfficeLevel(),marketModule.baseData.getOfficeNo(),3,marketModule.baseData.getPhysical(),marketModule.baseData.getOfficeSeatTime(),now));
                    MonopolyMarketMgr.sitOffice(marketModule,resp.getStcMsg().getSeatInfo(),resp.getSeatLevel(),resp.getNo());
                }

                player.notifyListener(eGamePlayerEventType.MonopolyMarketJoinOfficeTimes.getValue(), 1);
            }else {
                // 撤回
                MonopolyMarketMgr.refreshUser(player,true,false);

                LogMgr.addMonopolyMarketOfficeUserSeatLogData(new MonopolyMarketOfficeUserSeatLogData(player.getUserId(),marketModule.baseData.getOfficeCurPos(),marketModule.baseData.getOfficeLevel(),marketModule.baseData.getOfficeNo(),2,marketModule.baseData.getPhysical(),marketModule.baseData.getOfficeSeatTime(),now));
                int oldValue = marketModule.baseData.getPhysical();
                MonopolyMarketMgr.resetOfficeSeat(marketModule);
                LogMgr.addMonopolyMarketUserPhysicalChangeLogData(new MonopolyMarketUserPhysicalChangeLogData(player.getUserId(),marketModule.baseData.getOfficeCurPos(),marketModule.baseData.getOfficeLevel(),marketModule.baseData.getOfficeNo(),2,oldValue,marketModule.baseData.getPhysical(),now));
            }
            player.getModule(MonopolyMarketModule.class).synRoleData();
        }
        player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_OFFICE_ACTION, resp.getStcMsg().toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
